home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / GLX / basics / gloverlay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  10.4 KB  |  406 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *
  19.  *  gloverlay.c  - draw a red grid in the overlay planes
  20.  *               - rotate the rainbox cube in the standard planes 
  21.  *                 via a rotate button widget.
  22.  *
  23.  *  Ivan Hajadi
  24.  *  12-June-1992
  25.  *
  26.  *  to compile:
  27.  *  cc -o gloverlay gloverlay.c -lXirisw  -lgl_s -lXm -lXt -lX11_s -lPW -lsun 
  28.  */
  29.  
  30. /* Motif includes */
  31. #include <Xm/Xm.h>
  32. #include <Xm/Form.h>
  33. #include <Xm/PushB.h>
  34.  
  35. /* GL widget include */
  36. #include <X11/Xirisw/GlxMDraw.h>
  37.  
  38. #include <stdio.h>
  39.  
  40.  
  41. GLXconfig rgb_mode[] = {
  42.     { GLX_NORMAL, GLX_RGB, TRUE },
  43.     { GLX_NORMAL, GLX_DOUBLE, TRUE },
  44.     { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
  45.     { GLX_OVERLAY, GLX_BUFSIZE, 2},
  46.     { 0,          0,       0,   }
  47. };
  48.  
  49. /* globals */
  50. XtAppContext app_context;
  51. XtWorkProcId work_procid = 0;
  52. Display *display;
  53. Window normalWindow;
  54. Window overlayWindow;
  55. Pixel overlay_red_pixel;
  56.  
  57. void drawscene(int, int);
  58. void drawOverlayScene();
  59. void installColormaps(Widget, Widget);
  60.  
  61. main(int argc, char** argv)
  62. {
  63.     Widget top, form, quit_but, anim_but;
  64.     Widget gl_widget;
  65.     int overlayPlns;
  66.  
  67.     /* Prototypes */
  68.  
  69.     void quitCB(Widget, XtPointer, XtPointer);
  70.     void ginitCB(Widget, XtPointer, XtPointer);
  71.     void exposeCB(Widget, XtPointer, XtPointer);
  72.     void overlayExposeCB (Widget, XtPointer, XtPointer);
  73.     void animateCB(Widget, XtPointer, XtPointer);
  74.     void set_overlaymap( Widget );
  75.  
  76.     Arg args[20];
  77.     int n;
  78.     
  79.     top = XtAppInitialize(&app_context, "Cube", 
  80.         NULL, 0, &argc, argv, NULL, NULL, 0
  81.     );
  82.     n=0;
  83.     XtSetArg(args[n], XmNwidth, 730); n++;
  84.     XtSetArg(args[n], XmNheight, 445); n++;
  85.     form = XmCreateForm(top, "form", args, n);
  86.     XtManageChild(form);
  87.     
  88.     /* Create GL widget */
  89.  
  90.     /* Check if we have enough overlay planes */
  91.     overlayPlns = getgdesc(GD_BITS_OVER_SNG_CMODE);
  92.  
  93.     if (! overlayPlns ) {
  94.         fprintf(stderr, "No overlay planes.\n");
  95.         exit(1);
  96.     }
  97.  
  98.     n = 0;
  99.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  100.     XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  101.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++; 
  102.     XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  103.     XtSetArg(args[n], XmNleftOffset, 20); n++;
  104.     XtSetArg(args[n], XmNrightOffset, 120); n++;
  105.     XtSetArg(args[n], XmNbottomOffset, 40); n++;
  106.     XtSetArg(args[n], XmNtopOffset, 40); n++;
  107.     XtSetArg(args[n], XmNwidth, 500); n++;
  108.     XtSetArg(args[n], XmNheight, 400); n++;
  109.     XtSetArg(args[n], GlxNglxConfig, rgb_mode); n++;
  110.     XtSetArg(args[n], GlxNuseOverlay, True); n++;
  111.  
  112.     gl_widget = GlxCreateMDraw(form, "gl_widget", args, n);
  113.     XtManageChild(gl_widget);
  114.     
  115.     /* Add callbacks */
  116.     
  117.     XtAddCallback(gl_widget, GlxNginitCallback, ginitCB, NULL);
  118.     XtAddCallback(gl_widget, GlxNexposeCallback, exposeCB, NULL);
  119.     XtAddCallback(gl_widget, GlxNoverlayExposeCallback, overlayExposeCB, NULL);
  120.     
  121.     n=0;
  122.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  123.     XtSetArg(args[n], XmNrightOffset, 10); n++;
  124.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  125.     XtSetArg(args[n], XmNbottomOffset, 10); n++;
  126.     XtSetArg(args[n], XmNtraversalOn, False); n++;
  127.     quit_but = XmCreatePushButton(form, " Quit ", args, n);
  128.     XtManageChild(quit_but);
  129.  
  130.     n=0;
  131.     XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
  132.     XtSetArg(args[n], XmNrightOffset, 10); n++;
  133.     XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
  134.     XtSetArg(args[n], XmNbottomOffset, 50); n++;
  135.     XtSetArg(args[n], XmNtraversalOn, False); n++;
  136.     anim_but = XmCreatePushButton(form, "Rotate", args, n);
  137.     XtManageChild(anim_but);
  138.     
  139.     XtAddCallback(quit_but, XmNactivateCallback, quitCB, NULL);
  140.     XtAddCallback(anim_but, XmNactivateCallback, animateCB, NULL);
  141.     
  142.     XtRealizeWidget(top);
  143.     set_overlaymap( gl_widget );
  144.  
  145.     /* Time to install the colormaps */
  146.     installColormaps(top, gl_widget);
  147.  
  148.     /* Main Loop */
  149.     XtAppMainLoop(app_context);
  150. }
  151.  
  152. void installColormaps(Widget toplevel_widget, Widget gl_widget)
  153. {
  154.     Window window_list[3];
  155.  
  156.     display = XtDisplay(toplevel_widget);
  157.     window_list[0] = overlayWindow;
  158.     window_list[1] = XtWindow(toplevel_widget);
  159.     window_list[2] = XtWindow(gl_widget);
  160.     XSetWMColormapWindows(display, XtWindow(toplevel_widget), window_list, 3);
  161. }
  162.  
  163. Boolean animate(XtPointer clientD)
  164. {
  165.     static int angle_x = 0, angle_y = 0;
  166.     angle_y += 75;
  167.     angle_x += 75;
  168.     
  169.     if ((angle_y >= 3600) || (angle_y <= -3600))
  170.         angle_y = 0;
  171.     if ((angle_x >= 3600) || (angle_x <= -3600))
  172.         angle_x = 0;
  173.  
  174.     GLXwinset(display, normalWindow);
  175.     drawscene(angle_x, angle_y);
  176.  
  177.     /* 
  178.         Return False so this WorkProc 
  179.         keeps getting called. 
  180.     */
  181.     return False;
  182. }
  183.  
  184. void animateCB(Widget w, XtPointer clientD, XtPointer callD)
  185. {
  186.     static Boolean started = False;
  187.     Arg args[1];
  188.     XmString xms;
  189.  
  190.     if (! started ) {
  191.         Boolean animate(XtPointer);
  192.         work_procid = XtAppAddWorkProc(app_context, animate, NULL);
  193.         started = True;
  194.         xms = XmStringCreateSimple(" Stop ");
  195.     } else {
  196.         XtRemoveWorkProc(work_procid);
  197.         started = False;
  198.         xms = XmStringCreateSimple("Rotate");
  199.     }
  200.  
  201.     /* Changed the button label */
  202.     XtSetArg(args[0], XmNlabelString, xms);
  203.     XtSetValues(w, args, 1);
  204.     XmStringFree(xms);
  205. }
  206.  
  207. void ginitCB(Widget gl_widget, XtPointer clientD, XtPointer callD)
  208. {
  209.     GlxDrawCallbackStruct *call_data = (GlxDrawCallbackStruct *) callD;
  210.  
  211.     display = XtDisplay(gl_widget);
  212.     normalWindow = call_data->window;
  213.     GLXwinset(display, normalWindow);
  214.         
  215.     shademodel(GOURAUD);
  216.     zbuffer(TRUE);
  217.     subpixel(TRUE);
  218.     lsetdepth(getgdesc(GD_ZMIN), getgdesc(GD_ZMAX));
  219.     perspective(450, 1, 1, 100);
  220. }
  221.  
  222. void set_overlaymap( Widget gl_widget )
  223. {
  224.     /* Create and setup overlay colormap entries */
  225.  
  226.     Colormap overlay_cmap;
  227.     XColor color;
  228.     unsigned long pixels_return[ 1 ];
  229.     XVisualInfo *gl_visual_info;
  230.  
  231.     XtVaGetValues(gl_widget,
  232.       GlxNoverlayWindow, &overlayWindow,
  233.       GlxNoverlayVisual, &gl_visual_info,
  234.       NULL);
  235.     overlay_cmap = XCreateColormap(display, overlayWindow,
  236.                      gl_visual_info->visual, AllocNone );
  237.  
  238.     XtVaSetValues( gl_widget, GlxNoverlayColormap, overlay_cmap, NULL );
  239.  
  240.     /* Allocate some color cells */
  241.     if (!XAllocColorCells( display, overlay_cmap, False, NULL, 0, 
  242.                pixels_return, 1 )) {
  243.       fprintf( stderr, "XAllocColorCells failed.\n" );
  244.       exit( 1 );
  245.     }
  246.  
  247.     /* set the color cell to red */
  248.     overlay_red_pixel = color.pixel = pixels_return[ 0 ];
  249.     color.blue  = 0;
  250.     color.green = 0;
  251.     color.red   = 0xffff;
  252.     color.flags = DoRed|DoGreen|DoBlue;
  253.     XStoreColor( display, overlay_cmap, &color );
  254.     
  255. }    
  256.  
  257. void exposeCB(Widget w, XtPointer clientD, XtPointer callD)
  258. {
  259.     GlxDrawCallbackStruct* call_data = (GlxDrawCallbackStruct*) callD;
  260.     GLXwinset(XtDisplay(w), call_data->window);
  261.     animate(NULL);
  262. }
  263.  
  264. void overlayExposeCB(Widget w, XtPointer clientD, XtPointer callD)
  265. {
  266.     GlxDrawCallbackStruct* call_data = (GlxDrawCallbackStruct*) callD;
  267.     GLXwinset(XtDisplay(w), call_data->window);
  268.     drawOverlayScene(call_data->width, call_data->height);
  269. }
  270.  
  271. void quitCB(Widget w, XtPointer clientD, XtPointer callD)
  272. {
  273.     exit(0);
  274. }
  275.  
  276. void drawOverlayScene(int w, int h)
  277. {
  278.     int xoffset = w/5;
  279.     int yoffset = h/5;
  280.     int x1=0,y1=0, x2=0,y2=0;
  281.     register int i,j;
  282.  
  283.     pushmatrix();
  284.     ortho2(-0.1, (Coord)w+0.1, -0.1, (Coord)h+0.1);
  285.     reshapeviewport();
  286.     color(0);
  287.     clear();
  288.  
  289.     color(overlay_red_pixel); /* red */
  290.  
  291.     for (i=0;i<5;i++) {
  292.         x2 = xoffset;
  293.         y2 += yoffset;
  294.         for (j=0;j<5;j++) {
  295.             recti(x1,y1, x2,y2);
  296.             x1 += xoffset;
  297.             x2 += xoffset;
  298.         }
  299.         x1 = 0;
  300.         y1 += yoffset;
  301.     }
  302.     popmatrix();
  303. }
  304.  
  305. void drawscene(int angle_x, int angle_y)
  306. {
  307.     /* Draw smooth-shaded cube */
  308.     static long v1[4][3] = {
  309.         {-10, -10, 10},
  310.         {10, -10, 10},
  311.         {10, 10, 10},
  312.         {-10, 10, 10},
  313.     };
  314.     static long v2[4][3] = {
  315.         {-10, -10, -10},
  316.         {-10, 10, -10},
  317.         {10, 10, -10},
  318.         {10, -10, -10},
  319.     };
  320.     static long colors1[] = {
  321.         0x000000FF,
  322.         0x00FF0000,
  323.         0x0000FF00,
  324.         0x00FFFF00,
  325.     };
  326.     static long colors2[] = {
  327.         0x0000FFFF,
  328.         0x00FF00FF,
  329.         0x00FFFFFF,
  330.         0x0FF00000,
  331.     };
  332.     
  333.     register int i;
  334.     
  335.     reshapeviewport();
  336.     czclear(0x00777777, getgdesc(GD_ZMAX));
  337.     pushmatrix();
  338.     polarview(80, 0, 250, 0);
  339.     
  340.     pushmatrix();
  341.     rotate(angle_x, 'x');
  342.     rotate(angle_y, 'y');
  343.     
  344.     bgnpolygon();
  345.         for (i=0; i<4; i++) {
  346.             cpack(colors1[i]);
  347.             v3i(v1[i]);
  348.         }
  349.     endpolygon();
  350.     bgnpolygon();
  351.         for(i=0; i<4; i++) {
  352.             cpack(colors2[i]);
  353.             v3i(v2[i]);
  354.         }
  355.     endpolygon();
  356.     
  357.  
  358.     bgnpolygon();
  359.         cpack(colors1[1]);
  360.         v3i(v1[1]);
  361.         cpack(colors1[2]);
  362.         v3i(v1[2]);
  363.         cpack(colors2[2]);
  364.         v3i(v2[2]);
  365.         cpack(colors2[3]);
  366.         v3i(v2[3]);
  367.     endpolygon();
  368.     bgnpolygon();
  369.         cpack(colors1[0]);
  370.         v3i(v1[0]);
  371.         cpack(colors1[3]);
  372.         v3i(v1[3]);
  373.         cpack(colors2[1]);
  374.         v3i(v2[1]);
  375.         cpack(colors2[0]);
  376.         v3i(v2[0]);
  377.     endpolygon();
  378.  
  379.     bgnpolygon();
  380.         cpack(colors1[2]);
  381.         v3i(v1[2]);
  382.         cpack(colors1[3]);
  383.         v3i(v1[3]);
  384.         cpack(colors2[1]);
  385.         v3i(v2[1]);
  386.         cpack(colors2[2]);
  387.         v3i(v2[2]);
  388.     endpolygon();
  389.     bgnpolygon();
  390.         cpack(colors1[1]);
  391.         v3i(v1[1]);
  392.         cpack(colors1[0]);
  393.         v3i(v1[0]);
  394.         cpack(colors2[0]);
  395.         v3i(v2[0]);
  396.         cpack(colors2[3]);
  397.         v3i(v2[3]);
  398.     endpolygon();
  399.  
  400.     popmatrix();
  401.     popmatrix();
  402.     
  403.     swapbuffers();
  404. }
  405.  
  406.